package com.peanut.baby.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.peanut.baby.R;

/**
 * Created by anonymouslystudio on 2017/7/18.
 */

public class LoginBgLinear extends LinearLayout{
    private Paint outerPaint;
    private Paint innerPaint;
    int color;
    int smallRectSize;
    int stokenBold,stokenSlim;
    public LoginBgLinear(Context context) {
        super(context);
        init();
    }

    public LoginBgLinear(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoginBgLinear(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setWillNotDraw(false);
        smallRectSize = dip2px(getContext(),30);
        color = getContext().getResources().getColor(R.color.divider);

        stokenBold = dip2px(getContext(),1);
        stokenSlim = dip2px(getContext(),0.5f);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //fist draw out big frame
        canvas.drawRect(0,0,getWidth()- stokenBold,getHeight() - stokenBold,getOuterPaint());
        //draw four conner rect
        canvas.drawRect(0,0,smallRectSize,smallRectSize,getOuterPaint());
        canvas.drawRect(getWidth() - smallRectSize,0,getWidth(),smallRectSize,getOuterPaint());
        canvas.drawRect(getWidth() - smallRectSize,getHeight() - smallRectSize,getWidth(),getHeight()-stokenBold,getOuterPaint());
        canvas.drawRect(0,getHeight() - stokenBold - smallRectSize,smallRectSize,getHeight()-stokenBold,getOuterPaint());
        //draw inner frame
        canvas.drawRect(smallRectSize / 2,smallRectSize / 2, getWidth() - (smallRectSize / 2),getHeight() - (smallRectSize / 2),getInnerPaint());
    }

    private Paint getOuterPaint(){
        if(outerPaint == null){
            outerPaint = new Paint();
            outerPaint.setStyle(Paint.Style.STROKE);
            outerPaint.setStrokeWidth(stokenBold);
            outerPaint.setAntiAlias(true);
            outerPaint.setColor(color);
        }
        return outerPaint;
    }

    private Paint getInnerPaint(){
        if(innerPaint == null){
            innerPaint = new Paint();
            innerPaint.setStyle(Paint.Style.STROKE);
            innerPaint.setStrokeWidth(stokenSlim);
            innerPaint.setAntiAlias(true);
            innerPaint.setColor(color);
        }
        return innerPaint;
    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
